/* $Header: monitor.c,v 2.2 01/10/02 10:33:14 przybyls Exp $ */

/***********************************************************************/
/*                                                                     */
/*                  Copyright Hewlett-Packard Co. 1991                 */
/*                                                                     */
/***********************************************************************/

/***********************************************************************/
/* MODULE NAME:  monitor.c                                             */
/* ============                                                        */
/*                                                                     */
/* Author:  Lesley Freed                                               */
/*                                                                     */
/* MODULE PURPOSE:  To monitor the status of the instrument.           */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/*                                                                     */
/***********************************************************************/


/*****************************/
/*   GLOBAL INCLUDE FILES    */
/*****************************/

#include <typedefs.h>
#include <p_wkfile.h>
#include <wkfile.h>
#include <ramps.h>
#include <zone_config.h>
#include <zone_status.h>
#include <p_status.h>
#include <status.h>
#include <stddef.h>
#include <std_ops.h>
#include <task_pub.h>
#include <i_op.h>
#include <leds.h>
#include <iomap.h>
#include <exception.h>
#include <err_handler.h>
#include <error.h>
#include <glp.h>
#include <detector.h>
#include <det_ui.h>
#include <flow.h>
#include <oven.h>
#include <readiness.h>
#include <512.h>
#include <display.h>
#include <valve.h>
#include <recovery_ui.h>
#include <messages.h>
#include <p_ui.h>
#include <cmd_set.h>
#include <mio.h>
#include <als.h>
#include <flash.h>

/*****************************/
/*   LOCAL INCLUDE FILES     */
/*****************************/


/*****************************/
/*   LOCAL DEFINES           */
/*****************************/


/*****************************/
/*   GLOBAL VARIABLES        */
/*****************************/


/*****************************/
/*   EXTERNAL VARIABLES      */
/*****************************/


/*****************************/
/*   STATIC VARIABLES        */
/*****************************/



/*****************************/
/*   IMPLEMENT SECTION       */
/*****************************/

void StatusMonitor(void)
{

   INT32 time_duration;

   /* Keep track of how long we have been in power-on testing */
   time_duration = Uptime_Clock;

   /* Wait for the zone configuration to complete and then flag that the      */
   /* power-on tests are complete.  This will enable tasks that affect the    */
   /* keyboard and display.  Also wait for detector offset measurement to     */
   /* complete, and then wake up the detector state machines.                 */

   while ( ( Zone_Power_On_Complete == FALSE )      ||
           ( Offset_Measurement_Complete == FALSE ) ||
           ( DataComm_Power_On_Complete == FALSE ) )
   {

      /* The offset measurement should take longer than the wattage sense.  If */
      /* the zone power on is complete change the display to offset measuremnt */

      if ( ( Zone_Power_On_Complete ) && ( Offset_Measurement_Complete == FALSE ) )
      {
         DisplayRomMessage( MSG_TESTING_DETECTORS );
      }

      /* The data communications initialization should complete before the zone*/
      /* and offset measurement.  Only display this if something has gone awry */
      /* and we are still doing the datacomm test or the Skip_Power_On_Test    */
      /* flag has requested us to skip the zone and offset measurements.       */
      /* If we are doing an oven power on test display that message.           */

      if ( ( Offset_Measurement_Complete ) && ( DataComm_Power_On_Complete == FALSE ) )
      {
         if ( Oven_Power_On_Test )
         {
            DisplayRomMessage( MSG_TESTING_OVEN );
         }
         else
         {
            DisplayRomMessage( MSG_TESTING_DATACOMM );
         }
      }

      /* See if we have been measuring detector offset for too long */
      /* 30 seconds is about twice as long as it should take */
      if (((Uptime_Clock - time_duration) > 30) && (Offset_Measurement_Complete == FALSE))
      {
         /* This must mean that the signal DSP has failed */
         SetNewException( SIG_DSP_FAULT, 0, 0 );
         /* Terminate offset measurements */
         Offset_Measurement_Complete = TRUE;
      }

      Task_yield();
   }

   /* Perform poweron detector state machine initialization. */
   DetPostOffsetMeasurementInit();

   /* Enable the Control outputs on the CPF's OIM board */
   /*  this must occur after 512 writes current setpoints to OIM board */
   if ( GetCPFOIMPresence(FRONT) ) CPFCtlBdOnOff(FRONT,ON);
   if ( GetCPFOIMPresence(BACK) ) CPFCtlBdOnOff(BACK,ON);

   Skip_Power_On_Test     = FALSE;  /* Reset previous request to skip select power-on tests */
   Power_On_Test_Complete = TRUE;   /* Flag that we are through with the power-on tests and */
                                    /* can start normal operation.                          */
   ENABLE_START_RUN_INTERRUPT       /* If an external start interrupt was ignored during    */
                                    /* power-on, this will re-enable that interrupt.        */

   while ( TRUE )
   {
      Task_yield();

/**********************************************************/
/* Turn on ALS power and monitor for overcurrent.         */
/*                                                        */
/**********************************************************/

      MonitorSamplerPower();

/**********************************************************/
/* Turn oven on after small zones, flow are ready.        */
/* (Power fail recovery and method load, if oven ON).     */
/**********************************************************/

      HandleOvenHoldOff();

/******************************/
/* Update the valve readiness */
/******************************/

      EvaluateValveReadiness();

/**********************************************************/
/* Set the Not-Ready LED to correspond to readiness state */
/**********************************************************/

      SetNotReadyLedState();

/************************************************/
/* Check for oven at full power during a ramp   */
/************************************************/

      INTERRUPTS_OFF
      if ( ( Zone_Status.zones_at_max_power & OVEN_ON_MASK ) &&
           ( Oven_Table.ramp_info->ramp_state == RAMP ) &&
           ( Run_State == RUN_ACTIVE ) )
      {
 //        BLINK_RAMP_LED();
      }
      else
      {
 //        BLINK_RAMP_LED_OFF();
      }
      INTERRUPTS_ON

/****************************/
/* Check for Run Deviations */
/****************************/

      if ( Run_State == RUN_ACTIVE )
      {
         /* Log any possible exceptions present at the start of the */
         /* run.                                                    */
         LogPendingExceptions();
      }

      if ( DEVIATIONS_FOUND )
      {
 //           TURN_DEVIATION_LED_ON();            /* Deviaitions Found */
      }
      else
      {
//            TURN_DEVIATION_LED_OFF();           /* No Deviaitions Found */
      }

/***************************************/
/* Check for exception display request */
/***************************************/

      CheckForExceptionDisplay();

/***************************************/
/* Update exception log                */
/***************************************/

      UpdateExceptionLog();

/***********************************/
/* Check for popup display request */
/***********************************/

      CheckForPopupDisplay();

/********************************************************************/
/* Run detector subtask for flame ignition, set target offset, etc. */
/********************************************************************/

      DetectorSubtask();

/***************************************/
/* Check for dead Pneu DSP             */
/***************************************/

      CheckForDeadPcb();

/***************************************/
/* Check for Cleared H2 Safety Shutdown*/
/***************************************/

      ClearPneuExceptions();

/***************************************/
/* Check for dead mux adc              */
/***************************************/

      if ( Monitor_Tick_512_Counter > Tick_512_Counter )
      {
         /* Clock has rolled over */
         time_duration = 0xffff-Monitor_Tick_512_Counter + Tick_512_Counter;
      }
      else
      {
         time_duration = Tick_512_Counter - Monitor_Tick_512_Counter;
      }

      if ( time_duration > 10 )
      {
         BackUpMfgRAM();        /* if Mfg section has changed, recopy it */
         CheckForDeadMuxAdc();
         Monitor_Tick_512_Counter = Tick_512_Counter;
      }

/******************************************/
/* Check for manual split/splitless inlet */
/******************************************/

      if ( GetInletType( pFRONT_INLET ) == MANUAL_SS )
      {
         Zone_Status.cryo_valves_used |= SPLIT_VLVA_ON;
      }
      else
      {
         Zone_Status.cryo_valves_used &= SPLIT_VLVA_OFF;
      }

      if ( GetInletType( pBACK_INLET ) == MANUAL_SS )
      {
         Zone_Status.cryo_valves_used |= SPLIT_VLVB_ON;
      }
      else
      {
         Zone_Status.cryo_valves_used &= SPLIT_VLVB_OFF;
      }

   }  /* End While Loop */
}

